
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
    *returnSize = 0;
    if(root == NULL)
        return NULL;
    struct TreeNode *queue[2000];
    int **res = malloc(sizeof(int*)*2000);
    *returnColumnSizes = malloc(sizeof(int*)*2000);
    int rear = 0, front = 0, count = 0;
    queue[rear++] = root;
    while(front != rear){
        int len = rear - front;
        count = 0;
        res[*returnSize] = malloc(sizeof(int)*(len));
        for(int i = 0; i < len; i++){
            struct TreeNode *temp = queue[front++];
            res[*returnSize][count++] = temp->val;
            if(temp->left)
                queue[rear++] = temp->left;
            if(temp->right)
                queue[rear++] = temp->right;
        }
        (*returnColumnSizes)[*returnSize] = count;
        (*returnSize)++;
    }
    return res;
}
